
//添加服务内容规则
function addSerContentRuleInAdd(obj) {
	if(!$("#saveServiceContentForm").validationEngine("validate")){
		return false;
	} 
	var ruleUnitCode = $("#saveServiceContentForm button[name=ruleUnitMenu]").attr("value");
	var ruleUnitMsg = $("#saveServiceContentForm button[name=ruleUnitMenu]").text();
	
	var len = $(".gz-item-"+ruleUnitCode).length;
	var intN;
	if (len == 0) {
		intN = 1
	} else {
		intN = $(".gz-item-"+ruleUnitCode).eq(len - 1).find(".gznumber-"+ruleUnitCode).text();
		intN = parseInt(intN) + 1;
	}
	var html = '<div class="form-group ruleItem gz-item-'+ruleUnitCode+'" style="display: block;">'
			+ '<label class="control-label" style="width:100px">规则<font class="gznumber-'+ruleUnitCode+'">'
			+ intN
			+ '</font>：</label>'
			+ ' <div class="inline font14">时长：'
			+ ' <input type="text" class="form-control inline validate[required,custom[integer],min[1],max[24]]" style="width:60px;" name="ruleNum-'+ruleUnitCode+'-'+intN+'" id="ruleNum-'+ruleUnitCode+'"/>小时'
			+ '</div>'
			+ ' <div class="inline font14">费用：'
			+ ' <input type="text" class="form-control inline validate[required,custom[number],min[0.01]]" style="width:60px;" name="rulePrice-'+ruleUnitCode+'-'+intN+'" id="rulePrice-'+ruleUnitCode+'"/>元/'+ruleUnitMsg+''
			+ '</div>'
			+ ' <button type="button" class="btn btn-linkr ver_top" onclick="deleteContentRuleInAdd(this,\''+ruleUnitCode+'\')">删除</button>'
			+ '</div>';
	$(obj).parent().before(html);
	$("#serviceContentAddScroll").scrollTop(999999);
}

//删除服务内容规则
function deleteContentRuleInAdd(obj,ruleUnitCode) {
	$(obj).parents('.gz-item-'+ruleUnitCode).remove();
	var len = $(".gz-item-"+ruleUnitCode).length;
	for (var i = 0; i < len; i++) {
		var st = i+1;
		$(".gz-item-"+ruleUnitCode).eq(i).find(".gznumber-"+ruleUnitCode).text(st)
		$(".gz-item-"+ruleUnitCode).eq(i).find("#ruleNum-"+ruleUnitCode).attr("name", "ruleNum-"+ruleUnitCode+"-"+st)
		$(".gz-item-"+ruleUnitCode).eq(i).find("#rulePrice-"+ruleUnitCode).attr("name", "rulePrice-"+ruleUnitCode+"-"+st)
	}
}

// 根据单位选择显示的规则
function showContentRulesInAdd(obj,ruleUnitCode,ruleUnitMsg) {
	var ruleItemLen = $(".ruleItem").length;
	for (var i = 0; i < ruleItemLen; i++) {
		$(".ruleItem").eq(i).css("display", "none")
	}
	
	var len = $(".gz-item-"+ruleUnitCode).length;
	if (len==0){
		var html = '<div class="form-group ruleItem gz-item-'+ruleUnitCode+'" style="display: block;">'
				+ '<label class="control-label" style="width:100px">规则<font class="gznumber-'+ruleUnitCode+'">1'
				+ '</font>：</label>'
				+ ' <div class="inline font14">时长：'
				+ ' <input type="text" class="form-control inline validate[required,custom[integer],min[1],max[24]]" style="width:60px;" name="ruleNum-'+ruleUnitCode+'-1" id="ruleNum-'+ruleUnitCode+'"/>小时'
				+ '</div>'
				+ ' <div class="inline font14">费用：'
				+ ' <input type="text" class="form-control inline validate[required,custom[number],min[0.01]]" style="width:60px;" name="rulePrice-'+ruleUnitCode+'-1" id="rulePrice-'+ruleUnitCode+'"/>元/'+ruleUnitMsg+''
				+ '</div>'
				+ ' <button type="button" class="btn btn-linkr ver_top" onclick="deleteContentRuleInAdd(this,\''+ruleUnitCode+'\')">删除</button>'
				+ '</div>';
		$(".addRuleContinue").before(html);
	}else{
		for (var i = 0; i < len; i++) {
			$(".gz-item-"+ruleUnitCode).eq(i).css("display", "block")
		}
	}
}

// 提交表单
function saveServiceContentSubmitInAdd() {
	// 服务类型
	var serviceTypeCode = $("#saveServiceContentForm button[name=serviceTypeMenu]").attr("value");
	$("#saveServiceContentForm input[name=serviceTypeCode]").val(serviceTypeCode);
	if (serviceTypeCode=="") {
		zhyAlert("请选择服务类型")
		return false;
	}

	// 服务内容
	var serviceContentName = $("#saveServiceContentForm input[name=serviceContentName]").val();
	if (serviceContentName=="") {
		zhyAlert("服务内容不能为空")
		return false;
	}else if (serviceContentName.length > 8) {
		zhyAlert("服务内容长度不能超过8个字符")
		return false;
	}

	// 校验所填写的服务内容是否与服务类型中以前的有重复
	var serviceContentName = $("#saveServiceContentForm input[name=serviceContentName]").val();
	$.post("../service/check_service_content", {serviceTypeCode : serviceTypeCode, serviceContentName : serviceContentName}, function (data) {
    	if (data.result=="FAIL"){
			zhyAlert(data.msg)
			return false;
		}else {
			// 单位
			var ruleUnitCode = $("#saveServiceContentForm button[name=ruleUnitMenu]").attr("value");
			$("#saveServiceContentForm input[name=ruleUnitCode]").val(ruleUnitCode);
			if (ruleUnitCode=="") {
				zhyAlert("请选择规则类型")
				return false;
			}
			// 规则数量
			var ruleNum = $(".gz-item-"+ruleUnitCode).length;
			$("#saveServiceContentForm input[name=ruleNum]").val(ruleNum);
			if (ruleNum==0) {
				zhyAlert("请添加至少一条规则")
				return false;
			}

			$("#serviceContentAddScroll").scrollTop(999999);
			if(!$("#saveServiceContentForm").validationEngine("validate")){
				return false;
			} 
			
			$("#saveServiceContentForm").submit();
		}
    }, "json");	
	
}